package com.regentsoft.cloudback.operateAdmin.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.regentsoft.cloudback.base.BaseResp;
import com.regentsoft.cloudback.base.ResponseCode;
import com.regentsoft.cloudback.operateAdmin.entity.Application;
import com.regentsoft.cloudback.operateAdmin.service.ApplicationService;
import com.regentsoft.cloudback.vo.ErpVo;
import com.regentsoft.cloudback.vo.req.ApplicationAddReq;
import com.regentsoft.cloudback.vo.resp.GetErpListResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Api
@RestController
@RequestMapping("manager/application/")
public class ApplicationController {
	@Autowired
	private ApplicationService applicationService;
	
	@ApiOperation(value = "添加或修改企业配置信息", httpMethod = "POST", response = BaseResp.class, notes = "添加或修改企业配置信息")
	@RequestMapping(value="addOrEditApplication")
    public BaseResp addOrEditApplication(@RequestParam String token,@RequestBody ApplicationAddReq req) throws Exception {
		BaseResp resp  = new BaseResp();
		
		if(req.getCode()==null||req.getLink()==null||req.getTypeId()==null||req.getAccount()==null||req.getPassword()==null) {
			resp.setCode(ResponseCode.PARAMETERERR);
			resp.setMessage("填写数据不完整");
			return resp;
		}
		//添加或修改企业配置信息
		applicationService.addOrEditApplication(req.getCode(),req.getLink(),req.getTypeId(),req.getAccount(),req.getPassword());
		resp.setCode(ResponseCode.SUCCESS);
        return resp;
    }
	
	@ApiOperation(value = "获取erp类型", httpMethod = "GET", response = GetErpListResp.class, notes = "获取erp类型")
	@RequestMapping(value="getErpList")
    public GetErpListResp getErpList(@RequestParam String token) throws Exception {
		GetErpListResp resp  = new GetErpListResp();
		List<ErpVo> voList = null;
		try {
			List<Application> list = applicationService.findList(null);
			voList = new ArrayList<ErpVo>();
			for(Application app:list) {
				ErpVo vo = new ErpVo();
				vo.setId(app.getId());
				vo.setCode(app.getCode());
				voList.add(vo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		resp.setCode(ResponseCode.SUCCESS);
		resp.setData(voList);
        return resp;
    }
}
